package mybatis.main;

import static java.lang.System.out;

import java.io.Reader;
import java.sql.Date;

import mybatis.domain.User;
import mybatis.mapper.Mapper03;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

//mapper03.xml, SqlMapConfig.xml
public class Main03 {	
	public static void main(String[] args) throws Exception{		
		Reader reader = Resources.getResourceAsReader("config/SqlMapConfig.xml");		
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);		
		SqlSession sqlSession = sqlSessionFactory.openSession();
		
		Mapper03 mapper = sqlSession.getMapper(Mapper03.class);
		
		out.println("[delUserNull]");
		mapper.delUserNull();
		sqlSession.commit();
		
		out.println("[addUser]");
		User user01 = new User("u11", "u11",null, 10, Date.valueOf("2014-02-10"));
		mapper.addUser(user01);
		User user02 = new User("u12", "u12","백공일", null, Date.valueOf("2014-02-10"));		
		mapper.addUser(user02);
		User user03 = new User("u13", "u13","백공일", 10, null);		
		mapper.addUser(user03);		
		sqlSession.commit();
		
		out.println("[getUsers]");
		for(User user: mapper.getUsers()) out.println(user);

		out.println("[getUser]");		
		out.println(mapper.getUser("u01"));
	}
}